package update;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Hashtable;
import java.util.Iterator;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.multimedia.jolitorask.asterisk.AsteriskWrapperLocal;
import org.multimedia.jolitorask.asterisk.AsteriskWrapperLocalHome;
import org.multimedia.jolitorask.security.AuthServlet;
import org.multimedia.jolitorask.telephone.TelephoneLocal;
import org.multimedia.jolitorask.utilisateur.UtilisateurLocal;
import org.multimedia.jolitorask.utilisateur.UtilisateurLocalHome;

/**
 * Servlet implementation class for Servlet: UpdatePhone
 *
 */
 public class UpdatePhone extends AuthServlet implements javax.servlet.Servlet {
	 
   static final long serialVersionUID = 1L;
   private String name, newname, newpassword, login, account;
   private static final String JNDI_NAME_WRAPPER = "asteriskWrapperSessionLocal";
   
	public UpdatePhone() {
		super();
	}   	

	private void syntax_error(PrintWriter out){
		
		out.println(false);
	}
	
	@SuppressWarnings("unchecked")
	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

		res.setContentType("text/plain");
	    PrintWriter out = res.getWriter();
	    
	    account = req.getParameter("account");
	    login = req.getParameter("login");
	    name = req.getParameter("name");
	    newname =req.getParameter("newname");
	    newpassword = req.getParameter("newpassword");
	    
	    if(account==null || login==null || name==null){
	    	syntax_error(out);
	    	return;
	    }
	    
	    Context initContext = null;
	    Hashtable env = new Hashtable(1);
	    UtilisateurLocalHome utilisateurLocalHome = null;
	    UtilisateurLocal utilisateurLocal;
	    
	    if( !authentificate(req)){
	    	out.println(false);
	    	return;
	    }
	   
	    try{
	    	
	    	initContext = new javax.naming.InitialContext(env);	 
	    	utilisateurLocalHome = (UtilisateurLocalHome)initContext.lookup("utilisateurLocal");
	    	Iterator<UtilisateurLocal> ite = utilisateurLocalHome.findByClientAndLogin(account, login).iterator();
	    	
	    	if( ite.hasNext() )
	    		utilisateurLocal = ite.next();
	    	else {
	    		syntax_error(out);
	    		return;
	    	}
	    	
	    	Iterator<TelephoneLocal> telephones = utilisateurLocal.getTelephones().iterator();
	    	TelephoneLocal telephone = null, tmp;
	    	
	    	while(telephones.hasNext()){
	    		
	    		tmp = telephones.next();
	    		
	    		if( newname != null ){
	    			
	    			if( tmp.getNom().equals(newname) && !tmp.getNom().equals(name) ){
	    				syntax_error(out);
	    				return;
	    			}
    				
	    		}
	    		if( tmp.getNom().equals(name) )
	    			telephone = tmp;

	    	}
	    		
	    	if(telephone == null ){
	    		syntax_error(out);
	    		return;
	    	}
	    		
	    	if( newname != null )
	    		telephone.setNom(newname);
	    	if( newpassword != null )
	    		telephone.setPassword(newpassword);
    		
	    	//Objet Asterisk
	    	Object beanObject = null;
	    	try{
				beanObject = initContext.lookup(JNDI_NAME_WRAPPER);
				AsteriskWrapperLocalHome homeA = (AsteriskWrapperLocalHome)beanObject;
				AsteriskWrapperLocal wrapper = homeA.create();
				wrapper.init(account, login);
				if(newpassword != null ){
					wrapper.updatePhoneBuddy( ((Integer)telephone.getPrimaryKey()).intValue(), newpassword);
				}
	    	}catch (NamingException e) {
	    		out.println(false);
	    		return;
			}
	    	out.println(true);
	    	
	    	
	    }catch(Exception e){
	    	syntax_error(out);
	    	out.println(e.getMessage());
	    	return;
	    }
	    
	}  	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}   	  	    
}